(function(){
	var obj ={};
	obj.ajax = function(type, url, data, headers, context, callback){
		$.ajax({type:type, url:url, data: JSON.stringify(data), headers:headers, context:context})
		.done(callback);
	};
	window.j = obj;
})();

function commentsComponent(template){
	var model = [];
	var starList = [];
	var template = template;

	this.loadAllComment = function(){
		j.ajax("GET", "getOrderByMerchantId", null, null, this, function(m){
			model = m;
			j.ajax("GET", "showStar", null, null, this, function(getStar){
				starList = getStar;
				_render_comment(starList, model);
			});
		});
	};

	function _render_comment(starList, data){
		template.empty();
		if(data.length==0)
			return;
		let j = 0;
		data.forEach(function(item, index){
			var star = $("#star").clone().removeAttr("style");
			star.attr("id", "star" + j);
			j++;
			$("<tr/>").append($("<td/>").text(item.content))
			.append($("<td/>").text(getMyDate(item.comentDate)))
			.append($("<td/>").append(star))
			.appendTo(template);
		});
		for(var i = 0; i < data.length; i++){
			for(var t = 0; t < starList[i]; t++){
				$('#star'+i).children().eq(t).css("color", "red");
			}
		}
	}

	function getzf(num){  
		if(parseInt(num) < 10){  
			num = '0' + num;  
		}  
		return num;  
	}  
	function getMyDate(str){  
		var oDate = new Date(str),  
		oYear = oDate.getFullYear(),  
		oMonth = oDate.getMonth()+1,  
		oDay = oDate.getDate(),  
		oHour = oDate.getHours(),  
		oMin = oDate.getMinutes(),  
		oSen = oDate.getSeconds(),  
		oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
		return oTime;  
	}
}

$(function(){
	var c = new commentsComponent($("#comments"));
	c.loadAllComment();
});
	